package com.md.youyouhui.ui.pay;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.ActivitySelectOilBinding;
import com.md.youyouhui.databinding.AdapterSelectOilBinding;
import com.md.youyouhui.net.response.UserCardResponse;
import com.md.youyouhui.ui.BaseActivity;
import com.md.youyouhui.ui.adapter.DataBoundViewHolder;
import com.md.youyouhui.ui.user.AddCardActivity;
import com.md.youyouhui.utils.ToastUtils;
import com.md.youyouhui.view.MainViewModel;

import java.util.List;

/**
 * Created by AdministratorZJ
 * on 2019/4/13.
 */
public class SelectOilActivity extends BaseActivity {
    private ActivitySelectOilBinding oilBinding;
    private int select = -1;
    private MainViewModel viewModel;
    private MyAdapter adapter;
    private String id, num, type;

    @Override
    public void setContentLayout() {
        oilBinding = DataBindingUtil.setContentView(this, R.layout.activity_select_oil);
        viewModel = new MainViewModel();
    }

    @Override
    public void initView() {
        setSupportActionBar(oilBinding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void initData() {
        adapter = new MyAdapter();
        oilBinding.selectRecycler.setAdapter(adapter);
        oilBinding.selectBtn.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.putExtra("id", id);
            intent.putExtra("num", num);
            intent.putExtra("type", type);
            setResult(RESULT_OK, intent);
            finish();
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getUserCard();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_add_card, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.add:
                startActivity(new Intent(this, AddCardActivity.class));
                return true;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void getUserCard() {
        viewModel.getUserCard(1, 100).observe(this, userCardResponse -> {
            assert userCardResponse != null;
            switch (userCardResponse.status) {
                case LOADING:
                    oilBinding.progress.setVisibility(View.VISIBLE);
                    break;
                case SUCCESS:
                    oilBinding.progress.setVisibility(View.GONE);
                    UserCardResponse data = userCardResponse.data;
                    if (null == data) {
                        return;
                    }
                    List<UserCardResponse.Rows> rows = data.rows;
                    adapter.change(rows);
                    break;
                case ERROR:
                    oilBinding.progress.setVisibility(View.GONE);
                    ToastUtils.errMake(this, userCardResponse.errorCode);
                    break;
                default:
                    break;
            }
        });
    }

    class MyAdapter extends RecyclerView.Adapter<DataBoundViewHolder<AdapterSelectOilBinding>> {
        private List<UserCardResponse.Rows> rows;

        private void change(List<UserCardResponse.Rows> rows) {
            this.rows = rows;
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public DataBoundViewHolder<AdapterSelectOilBinding> onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            AdapterSelectOilBinding binding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.adapter_select_oil, viewGroup, false);
            return new DataBoundViewHolder<>(binding);
        }

        @Override
        public void onBindViewHolder(@NonNull DataBoundViewHolder<AdapterSelectOilBinding> holder, int i) {
            if (select == i) {
                holder.binding.selectCheck.setChecked(true);
            } else {
                holder.binding.selectCheck.setChecked(false);
            }
            holder.binding.selectId.setText(rows.get(i).cardNo);
            if ("1".equals(rows.get(i).type)) {
                holder.binding.selectTitle.setText("中石化");
                holder.binding.cardBg.setBackgroundResource(R.drawable.shihua);
            } else {
                holder.binding.selectTitle.setText("中石油");
                holder.binding.cardBg.setBackgroundResource(R.drawable.shiyou);
            }
            holder.binding.selectCheck.setOnCheckedChangeListener((buttonView, isChecked) -> {
                if (!holder.binding.selectCheck.isPressed()) {
                    return;
                }
                if (isChecked) {
                    select = i;
                    id = rows.get(i).id;
                    num = rows.get(i).cardNo;
                    type = rows.get(i).type;
                    notifyDataSetChanged();
                    oilBinding.selectBtn.setEnabled(true);
                } else {
                    select = -1;
                    oilBinding.selectBtn.setEnabled(false);
                }
            });
        }

        @Override
        public int getItemCount() {
            return null == rows ? 0 : rows.size();
        }
    }
}
